

from pin_item import Pin

class PortItem:

    def __init__(self,type="input",width=1,name="I", parent = None) -> None:
        assert(type in ["input","output"])
        self.used = False
        self.type = type
        self.width = width
        self.name = name
        self.parent = parent
        self.pins = []
        if width==1:
            self.pins.append(Pin(name,self))
        else:
            for i in range(width):
                self.pins.append(Pin(name+'['+str(i)+']', self))

        pass
    def get_width_str(self):
        if self.width==1:
            return ""
        else:
            return "[{}:0]".format(self.width-1)
    
    def get_name(self):
        return self.name
    def get_type(self):
        return self.type
    
    def get_pin_by_index(self,index):
        return self.pins[index]
    def get_pin(self):
        return self.pins[0]
    def get_all_pins(self):
        return self.pins
    def get_pins_bits(self):
        data = []
        for pin in self.pins:
            data.append(pin.get_bit())
        return data
    
    def set_parent(self,parent):
        self.parent = parent
    

